package com.pk.platform.business.flow.service.impl;

import java.util.List;

import com.pk.platform.business.flow.dao.IFlowDefineDao;
import com.pk.platform.business.flow.service.IFlowDefineService;
import com.pk.platform.business.flow.vo.WorkFlowVO;
import com.pk.platform.domain.flow.FlowModule;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class FlowDefineServiceImpl implements IFlowDefineService {

	private IFlowDefineDao flowDefineDao;
	
	public List<FlowModule> queryFlowModule() {
		return flowDefineDao.queryFlowModule();
	}
	
	public ListPage queryWorkFlow(WorkFlowVO wfvo, Pager pager)
	{
		return flowDefineDao.queryWorkFlow(wfvo, pager);
	}
	
	public FlowModule queryFlowModuleById(String id)
	{
		FlowModule flowModule = flowDefineDao.get(FlowModule.class, id);
		return flowModule;
	}
	
	public void saveFlowModule(FlowModule fm)
	{
		if(fm.getId() == null) //新增
		{
			flowDefineDao.add(fm);
		}
		else //修改
		{
			FlowModule flowModule = this.queryFlowModuleById(fm.getId());
			flowModule.setModuleName(fm.getModuleName());
			flowModule.setSort(fm.getSort());
		}
	}
	
	public String delFlowModule(String[] ids)
	{
		StringBuffer result = new StringBuffer("模块：");
		StringBuffer mStr = new StringBuffer();
		for(String id : ids)
		{
			FlowModule fm = this.queryFlowModuleById(id);
			if(fm.getWorkFlowSet().size() > 0)
			{
				mStr.append("，").append(fm.getModuleName());
			}
			else
			{
				flowDefineDao.delete(FlowModule.class, id);
			}
		}
		String s = mStr.toString();
		if(s.length() > 0)
		{
			s = s.substring(1);
		}
		else
		{
			return "";
		}
		result.append(s).append("已有流程定义，不能删除。");
		return result.toString();
	}

	public IFlowDefineDao getFlowDefineDao() {
		return flowDefineDao;
	}

	public void setFlowDefineDao(IFlowDefineDao flowDefineDao) {
		this.flowDefineDao = flowDefineDao;
	}

}
